package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonWriter;
import javax.json.stream.JsonGenerator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonSampleServlet extends HttpServlet {
	private static Logger LOGGER = LoggerFactory
			.getLogger(JsonSampleServlet.class);

	/**
	 * Constructor of the object.
	 */
	public JsonSampleServlet() {
		super();
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		LOGGER.debug("doGet");
		String method = request.getParameter("method");
		String otherText = request.getParameter("otherText");
		LOGGER.debug("method: {}", method);
		LOGGER.debug("otherText: {}", otherText);
		if ("zTree".equals(method)) {
			final PrintWriter writer = response.getWriter();
			final JsonGenerator generator = Json.createGenerator(writer);

			 
		}
		// RequestDispatcher dispatcher =
		// request.getRequestDispatcher("chartTree.jsp");
		// dispatcher.forward(request, response);
		final PrintWriter writer = response.getWriter();
		JsonWriter jwriter = Json.createWriter(writer);
		jwriter.writeObject(buildJSon());
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {
		LOGGER.debug("doPost");
		String method = request.getParameter("method");
		String otherText = request.getParameter("otherText");
		LOGGER.debug("method: {}", method);
		LOGGER.debug("otherText: {}", otherText);
		if ("zTree".equals(method)) {
			final PrintWriter writer = response.getWriter();
			final JsonGenerator generator = Json.createGenerator(writer);

		}
		// RequestDispatcher dispatcher =
		// request.getRequestDispatcher("chartTree.jsp");
		// dispatcher.forward(request, response);
		final PrintWriter writer = response.getWriter();
		JsonWriter jwriter = Json.createWriter(writer);
		jwriter.writeArray(buildJSonZTreeArray());

	}

	public static void main(String[] args) {

		StringWriter s = new StringWriter();
		JsonWriter writer = Json.createWriter(s);
		writer.writeObject(buildJSon());
		System.out.println(s);

	} 

	public static JsonArray buildJSonZTreeArray() {
		JsonArray value = null;
		value = Json
				.createArrayBuilder()
				.add(Json
						.createObjectBuilder()
						.add("name", "pNode 01")
						.add("open", true)
						.add("children",
								Json.createArrayBuilder()
										.add(Json
												.createObjectBuilder()
												.add("name", "pNode 11")
												.add("children",
														Json.createArrayBuilder()
																.add(Json
																		.createObjectBuilder()
																		.add("name",
																				"leaf node 111"))
																.add(Json
																		.createObjectBuilder()
																		.add("name",
																				"leaf node 112"))
																.add(Json
																		.createObjectBuilder()
																		.add("name",
																				"leaf node 113"))
																.add(Json
																		.createObjectBuilder()
																		.add("name",
																				"leaf node 114"))))))
				.add(Json.createObjectBuilder().add("name", "pNode 02")
						.add("open", true)
						.add("children", Json.createArrayBuilder()))
				.add(Json.createObjectBuilder()
						.add("name", "pNode 3 - no child")
						.add("isParent", true)).build();
		return value;
	}

	public static JsonObject buildJSonZTree() {
		JsonObject value = null;
		value = Json
				.createObjectBuilder()
				.add("name", "pNode 01")
				.add("open", true)
				.add("children",
						Json.createArrayBuilder()
								.add(Json
										.createObjectBuilder()
										.add("name", "pNode 11")
										.add("children",
												Json.createArrayBuilder()
														.add(Json
																.createObjectBuilder()
																.add("name",
																		"leaf node 111"))
														.add(Json
																.createObjectBuilder()
																.add("name",
																		"leaf node 112"))
														.add(Json
																.createObjectBuilder()
																.add("name",
																		"leaf node 113"))
														.add(Json
																.createObjectBuilder()
																.add("name",
																		"leaf node 114"))))
								.add(Json
										.createObjectBuilder()
										.add("name", "pNode 12")
										.add("children",
												Json.createArrayBuilder()
														.add(Json
																.createObjectBuilder()
																.add("name",
																		"leaf node 121"))
														.add(Json
																.createObjectBuilder()
																.add("name",
																		"leaf node 122"))
														.add(Json
																.createObjectBuilder()
																.add("name",
																		"leaf node 123"))
														.add(Json
																.createObjectBuilder()
																.add("name",
																		"leaf node 124"))))
								.add(Json.createObjectBuilder()
										.add("name", "pNode 13 - no child")
										.add("isParent", true))).build();

		return value;
	}

	public static JsonObject buildJSon() {
		JsonObject value = Json
				.createObjectBuilder()
				.add("id", "1234")
				.add("date", "19/09/2012")
				.add("total_amount", "93.48")
				.add("customer",
						Json.createObjectBuilder().add("first_name", "James")
								.add("last_name", "Rorrison")
								.add("email", "j.rorri@me.com")
								.add("phoneNumber", "+44 1234 1234"))
				.add("content",
						Json.createArrayBuilder()
								.add(Json.createObjectBuilder()
										.add("item", "H2G2")
										.add("unit_price", "23.5")
										.add("quantity", "1"))
								.add(Json.createObjectBuilder()
										.add("item", "Harry Potter")
										.add("unit_price", "34.99")
										.add("quantity", "2")))
				.add("credit_card",
						Json.createObjectBuilder()
								.add("number", "123412341234")
								.add("expiry_date", "10/13")
								.add("control_number", "234")
								.add("type", "Visa")).build();
		return value;
	}

	public static JsonObject buildJSonWithArray() {
		JsonObject itemArrayH2G2 = Json.createObjectBuilder()
				.add("item", "H2G2").add("unit_price", "23.5")
				.add("quantity", "1").build();

		JsonObject itemArrayPotter = Json.createObjectBuilder()
				.add("item", "Harry Potter").add("unit_price", "34.99")
				.add("quantity", "2").build();

		JsonObject value = Json
				.createObjectBuilder()
				.add("id", "1234")
				.add("date", "19/09/2012")
				.add("total_amount", "93.48")
				.add("customer",
						Json.createObjectBuilder().add("first_name", "James")
								.add("last_name", "Rorrison")
								.add("email", "j.rorri@me.com")
								.add("phoneNumber", "+44 1234 1234"))
				.add("content",
						Json.createArrayBuilder().add(itemArrayH2G2)
								.add(itemArrayPotter))
				.add("credit_card",
						Json.createObjectBuilder()
								.add("number", "123412341234")
								.add("expiry_date", "10/13")
								.add("control_number", "234")
								.add("type", "Visa")).build();
		return value;
	}

	public static JsonObject buildJSonWithArraySeperate() {

		JsonObject customerJames = Json.createObjectBuilder()
				.add("first_name", "James").add("last_name", "Rorrison")
				.add("email", "j.rorri@me.com")
				.add("phoneNumber", "+44 1234 1234").build();

		JsonObject itemArrayH2G2 = Json.createObjectBuilder()
				.add("item", "H2G2").add("unit_price", "23.5")
				.add("quantity", "1").build();

		JsonObject itemArrayPotter = Json.createObjectBuilder()
				.add("item", "Harry Potter").add("unit_price", "34.99")
				.add("quantity", "2").build();

		JsonObject creditCard = Json.createObjectBuilder()
				.add("number", "123412341234").add("expiry_date", "10/13")
				.add("control_number", "234").add("type", "Visa").build();

		JsonObject value = Json
				.createObjectBuilder()
				.add("id", "1234")
				.add("date", "19/09/2012")
				.add("total_amount", "93.48")
				.add("customer", customerJames)
				.add("content",
						Json.createArrayBuilder().add(itemArrayH2G2)
								.add(itemArrayPotter))
				.add("credit_card", creditCard).build();
		return value;
	}
}
